package com.util;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CapturePictureFromVideo extends Thread {

    private String VideoPath;
    private String PicturePath;

    public CapturePictureFromVideo(String videoPath, String picturePath) {
        VideoPath = videoPath;
        PicturePath = picturePath;
    }

    @Override
    public void run() {
        super.run();
        System.out.println("picture");
        File picture=new File(PicturePath);
        FFmpegFrameGrabber capture=new FFmpegFrameGrabber(VideoPath);
        try {
            capture.start();
            int length=capture.getLengthInFrames();
            int flag=0;
            Frame frame=null;
            while (flag<length){
                frame=capture.grabFrame();
                if(flag>100&&frame.image!=null)
                    break;
                flag++;
            }
            String rotate = capture.getVideoMetadata("rotate");
            Java2DFrameConverter converter = new Java2DFrameConverter();
            BufferedImage bi = converter.getBufferedImage(frame);
            Rectangle des;
            if(rotate!=null){
                int angel=Integer.parseInt(rotate);
                int origin_width=bi.getWidth();
                int origin_height=bi.getHeight();
                int type=bi.getColorModel().getTransferType();
                Rectangle rect_des = new Rectangle(new Dimension(origin_width, origin_height));
                if(Integer.parseInt(rotate)>=90){
                    if(angel/90%2==1){
                        int temp=rect_des.height;
                        rect_des.height=rect_des.width;
                        rect_des.width=temp;
                    }
                    angel=angel%90;
                }
                double r = Math.sqrt(rect_des.height * rect_des.height + rect_des.width * rect_des.width) / 2;
                double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
                double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
                double angel_dalta_width = Math.atan((double) rect_des.height / rect_des.width);
                double angel_dalta_height = Math.atan((double) rect_des.width / rect_des.height);
                int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width));
                int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height));
                int des_width = rect_des.width + len_dalta_width * 2;
                int des_height = rect_des.height + len_dalta_height * 2;
                des=new Rectangle(new Dimension(des_width,des_height));
                BufferedImage bi1 = new BufferedImage(des.width, des.height,1);
                Graphics2D g2 = bi1.createGraphics();
                g2.translate((des.width - origin_width) / 2, (des.height - origin_height) / 2);
                g2.rotate(Math.toRadians(angel), origin_width / 2, origin_height / 2);
                g2.drawImage(bi, 0, 0, null);
                g2.dispose();
                bi=bi1;
            }

            ImageIO.write(bi,"jpg",picture);
            capture.stop();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
